package com.bluedon.ism.commons.util;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 
 * 下载工具类
 */
public class DownLoadUtil {

	public static void download(HttpServletResponse response, InputStream in,
			long size, String fileName, String character, boolean isInline)
			throws IOException {
		response.reset();// 防止 BEA Weblogic 会报错
		try {
			String mimetype = "application/octet-stream;charset=utf-8";
			response.setContentType(mimetype);
			String inlineType = isInline ? "inline" : "attachment"; // 是否内联附件
			// fileName=new String(fileName.getBytes("iso8859-1"),
			// "utf-8");//乱码先还原为正常汉字
			fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");// 为下载文件名编码
			response.setHeader("Content-Disposition", inlineType
					+ ";filename=\"" + fileName + "\"");
			response.setContentLength((int) size);// 设置下载内容大小
			int buffer_size = 4096;
			byte[] buffer = new byte[buffer_size]; // 缓冲区
			BufferedOutputStream output = null;
			BufferedInputStream input = null;
			try {
				output = new BufferedOutputStream(response.getOutputStream());
				input = new BufferedInputStream(in);
				int n = -1;
				while ((n = input.read(buffer, 0, buffer_size)) > -1) {
					output.write(buffer, 0, n);
				}
				response.flushBuffer();
			} catch (Exception e) {
				// 用户可能取消了下载
				e.printStackTrace();
			} finally {
				if (null != input) {
					input.close();
				}
				if (null != output) {
					output.close();
				}
			}
			return;
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void download(HttpServletResponse response, File file,
			String fileName, String character, boolean isInline)
			throws IOException {
		response.reset();// 防止 BEA Weblogic 会报错
		try {
			String mimetype = "application/x-msdownload;charset=utf-8";
			response.setContentType(mimetype);
			String inlineType = isInline ? "inline" : "attachment"; // 是否内联附件
			// fileName=new String(fileName.getBytes("iso8859-1"),
			// "utf-8");//乱码先还原为正常汉字(linux)
			fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");// 为下载文件名编码(windows)
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ fileName + "\"");
			response.setContentLength((int) file.length());// 设置下载内容大小
			int buffer_size = 4096;
			byte[] buffer = new byte[buffer_size]; // 缓冲区
			BufferedOutputStream output = null;
			BufferedInputStream input = null;
			try {
				output = new BufferedOutputStream(response.getOutputStream());
				input = new BufferedInputStream(new FileInputStream(file));
				int n = -1;
				while ((n = input.read(buffer, 0, buffer_size)) > -1) {
					output.write(buffer, 0, n);
				}
				response.flushBuffer();
			} catch (Exception e) {
				// 用户可能取消了下载
				e.printStackTrace();
			} finally {
				if (null != input) {
					input.close();
				}
				if (null != output) {
					output.close();
				}
			}
			return;
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
